<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/7/12
 * Time: 17:47
 */
namespace app\admin\controller;
use think\Db;
use think\Request;

class GoodsTypeController extends BaseController{
    //产品类型列表
    public function lst(){
        //获取产品类型列表
        $type_list = Db::table('goods_type')->where('is_del != 1')->select();
        $this->assign('list',$type_list);
        return $this->fetch();
    }
    //添加产品类型
    public function add(){
        if(Request::instance()->isPost()){
        //接收数据
        $data = [
            'name'=>Request::instance()->param('name'),
        ];
        //验证数据
        $result = $this->validate($data,'GoodsType');
        //验证通过并且是已删除的
            if(true === $result){
               $type_name = Db::table('goods_type')->where('name',$data['name'])->find();
                if(isset($type_name) && $type_name['is_del']!=1){
                    $this->error('添加失败,名称已存在',url('GoodsType/add'));
                }else{
                    //验证成功添加
                    $insertData = [
                        'name'=>$data['name'],
                        'created_time'=>time(),
                        'updated_time'=>time(),
                    ];
                    $re = Db::table('goods_type')->insert($insertData);
                    if($re){
                        $this->success('添加成功',url('GoodsType/lst'));
                    }else{
                        $this->error('添加失败',url('GoodsType/add'));
                    }
                }
            }else{
                $this->error($result,url('GoodsType/add'));
            }

        }
        return $this->fetch();

    }
    //编辑产品类型
    public function edit(){
        $id = Request::instance()->param('id');
        $type = Db::table('goods_type')->where('id',$id)->find();
        if(Request::instance()->isPost()){
            //接收数据
            $data = [
                'name'=>Request::instance()->param('name'),
                'id'=>Request::instance()->param('id'),
            ];
            //验证数据
            $result = $this->validate($data,'GoodsType.edit');
            if(true === $result){
                $type_name = Db::table('goods_type')->where('name',$data['name'])->find();
                if($type_name && $type['name']!=$data['name'] && $type_name['is_del']!=1){
                    $this->error('类型名称已存在',url('GoodsType/lst'));
                }else{
                    //验证成功编辑
                    $updateData = [
                        'name'=>$data['name'],
                        'updated_time'=>time(),
                    ];
                    $re = Db::table('goods_type')->where('id',$id)->update($updateData);
                    if($re !==false){
                        $this->success('修改成功',url('GoodsType/lst'));
                    }else{
                        $this->error('修改失败',url('GoodsType/edit',['id'=>$id]));
                    }
                }

            }else{
                $this->error($result,url('GoodsType/edit',['id'=>$id]));
            }

        }


        $this->assign('type',$type);
        return $this->fetch();
    }
    //查看产品类型
    //删除产品类型
public function del(){
    $id=Request::instance()->param('id');
    $re = Db::table('goods_type')->where('id',$id)->update(['is_del'=>1]);
    if($re){
        $this->success('删除成功',url('GoodsType/lst'));
    }else{
        $this->error('删除成功',url('GoodsType/lst'));
    }
}
}